package com.drinks.yiniang.io.api;

import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.user.UserInfoJson;
import com.drinks.yiniang.entity.user.address.AddressInfoJson;
import com.drinks.yiniang.entity.user.address.DeliverAddressJson;
import com.drinks.yiniang.entity.user.wallet.RechargePackageJson;
import com.drinks.yiniang.entity.user.wallet.WalletInfoJson;
import com.drinks.yiniang.entity.user.wish.DrinksWishJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;

/**
 * 心愿单api
 * Created by han on 2019/3/6 0006.
 */

public class WishApiIO extends BaseApiIO {
    volatile static WishApiIO instance;

    static public WishApiIO getInstance() {
        if (instance == null) {
            synchronized (WishApiIO.class) {
                if (instance == null) {
                    instance = new WishApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 心愿单列表
     *
     * @param type     类型 1商品 2酒单 3他人酒单
     * @param page
     * @param callback
     */
    public void getWishList(String type, int page, final APIRequestCallback<GoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_WISH_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("心愿单列表===onSuccess");

                GoodsJson mDataJson = JSON.parseObject(res, GoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 心愿单我的酒单(我收藏的他人酒单)列表
     *
     * @param type     类型 1商品 2酒单 3他人酒单
     * @param page
     * @param callback
     */
    public void getMyWishWineList(String type, int page, final APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_WISH_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("心愿单我的酒单列表===onSuccess");

                DrinksWishJson mDataJson = JSON.parseObject(res, DrinksWishJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 心愿单其他用户的酒单列表
     *
     * @param userId   被查看的用户id
     * @param page
     * @param callback
     */
    public void getOtherWishWineList(String userId, int page, final APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("current_fans_id", SharedPreUtils.getInstance().getUserID());
        paramsMap.put("view_fans_id", userId);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_COMMUNITY_OTHER_WISH_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("心愿单他人的酒单列表===onSuccess");

                DrinksWishJson mDataJson = JSON.parseObject(res, DrinksWishJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 收藏心愿单
     *
     * @param id       酒单id
     * @param callback
     */
    public void doWishListCollect(String id, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", id);

        MainApiIO.getInstance().postRequest(HttpUrls.API_WINE_LIST_COLLECT, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("收藏心愿单===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 点赞心愿单
     *
     * @param id       酒单id
     * @param callback
     */
    public void doWishListLike(String id, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", id);

        MainApiIO.getInstance().postRequest(HttpUrls.API_WINE_LIST_LIKE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("点赞心愿单===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
